package com.satis.compose.get.example.pages

import android.util.Log
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.text.ClickableText
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Favorite
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.onGloballyPositioned
import androidx.compose.ui.layout.positionInWindow
import androidx.compose.ui.text.*
import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.satis.compose.get.example.ui.theme.ComposeGetTheme
import com.satis.compose.get.example.ui.theme.Purple200
import com.satis.compose.get.example.viewmodel.MainViewMode
import com.satis.compose.get.widget.GetPage
import com.satis.compose.get.widget.GetTopAppBar
import com.satis.compose.get.annotation.PageRoute
import com.satis.compose.get.navigator.LocalPageController


/**
 * @author sunshaobei on 2022/9/6R®®
 */
@Composable
@PageRoute(route = "/app/text")
fun TextPage() {
    val navigator = LocalPageController.current
    val viewMode:MainViewMode = viewModel()
    GetPage(statusBarColor = Color.Transparent, modifier = Modifier.onGloballyPositioned {

    }) {
        Scaffold(
            topBar = {
                GetTopAppBar(
                    title = { Text(text = "Text", color = Color.White) },
                    backgroundColor = Purple200
                )
            },
            floatingActionButton = {
                FloatingActionButton(onClick = {
                   navigator.push("/app/mvi")
                }, modifier = Modifier.navigationBarsPadding()) {
                    Icon(imageVector = Icons.Default.Favorite, contentDescription = null)
                }
            },
            floatingActionButtonPosition = FabPosition.End
        ) {
            LazyColumn(
                modifier = Modifier.padding(it),
                contentPadding = PaddingValues(horizontal = 10.dp),
                horizontalAlignment = Alignment.CenterHorizontally,
                verticalArrangement = Arrangement.spacedBy(20.dp)
            ) {

                item {
                    Column {
                        var state by remember {
                            mutableStateOf(false)
                        }
                        Text(text = "进来了", modifier = Modifier.onGloballyPositioned {
                            if (
                                it.positionInWindow().isValid()
                            ){
                                state = true
                            }
                        })

                        Text(text = "delete", textDecoration = TextDecoration.LineThrough)
                        Text(text = "underline", textDecoration = TextDecoration.Underline)
                    }
                }
                item {
                    Column(horizontalAlignment = Alignment.CenterHorizontally) {
                        Text(text = "字号 fontSize")
                        Text(text = "16sp", fontSize = 16.sp)
                        Text(text = "18sp", fontSize = 18.sp)
                        Text(text = "20sp", fontSize = 20.sp)
                        Text(text = "24sp", fontSize = 24.sp)
                    }
                }

                val annotatedString = buildAnnotatedString {
                    append("start")
                    pushStringAnnotation("private", "https://www.baidu.com")
                    withStyle(
                        style = SpanStyle(
                            fontSize = 30.sp,
                            color = Color.Red,
                            textDecoration = TextDecoration.Underline
                        )
                    ) {
                        append("center")
                    }
                    pop()
                    append("end")
                }

                item {
                    Column {
                        Text(text = "ClickableText")
                        ClickableText(
                            //                        style = TextStyle(fontSize = 24.sp),
                            text = annotatedString, onClick = {
                                annotatedString.getStringAnnotations("private", it, it)
                                    .firstOrNull()?.let { annotated ->
                                        Log.e("private", annotated.item)
                                    }
                            })
                    }
                }
            }
        }
    }
}


@Preview(showBackground = true)
@Composable
fun TextScreenPreview() {
    ComposeGetTheme {
        TextPage()
    }
}